package com.xuecheng.media.api;

import com.xuecheng.execption.XueChengPlusException;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.service.MediaFilesService;
import com.xuecheng.model.RestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author 小班哥
 * @version 1.0
 * @date 2023/4/1 15:27
 * @description 大文件上传接口
 */

@Slf4j
@RestController
@Api(value = "大文件上传接口" , tags = "大文件上传接口")
public class BigFilesController {


    @Autowired
    private MediaFilesService mediaFilesService;


    @ApiOperation(value = "文件上传前检查文件是否存在")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> UploadCheck(@RequestParam("fileMd5") String fileMd5){
        return mediaFilesService.FileUploadCheck(fileMd5);
//        return RestResponse.success(false);
    }

    @ApiOperation(value = "文分块件上传时检查文件是否存在")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> BlockFileCheck(@RequestParam("fileMd5") String fileMd5
            ,@RequestParam("chunk") int chunk){
        return mediaFilesService.UploadBlockFileCheck(fileMd5 , chunk);
    }


    @ApiOperation(value = "上传分块文件")
    @PostMapping("/upload/uploadchunk")
    public RestResponse BlockFileUpload(
            @RequestParam("file")MultipartFile file
            ,@RequestParam("fileMd5") String fileMd5
            ,@RequestParam("chunk") int chunk) throws IOException{

            File tempFile = File.createTempFile("_file", ".temp");
            file.transferTo(tempFile);
            String absolutePath = tempFile.getAbsolutePath();
            return mediaFilesService.UploadBlockFile(absolutePath , fileMd5 , chunk);
    }


    @ApiOperation(value = "合并分块视频")
    @PostMapping("/upload/mergechunks")
    public RestResponse MergeFile(
            @RequestParam("fileMd5") String md5
            , @RequestParam("fileName") String FileName
            , @RequestParam("chunkTotal") int FileCount){

        long companyId = 1232141425;
        UploadFileParamsDto dto = new UploadFileParamsDto();
        dto.setFilename(FileName);
        dto.setStatus("001002");
        RestResponse restResponse = mediaFilesService.MergedBranch(companyId, md5, FileCount, dto);
        return restResponse;
    }








}
